package com.example.heimashop.view.fragment;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.example.heimashop.base.BaseFragment;
import com.example.heimashop.databinding.FragmentMineBinding;
import com.example.heimashop.utils.SPUtils;
import com.example.heimashop.utils.ToastUtils;
import com.example.heimashop.view.activity.AddressListActivity;
import com.example.heimashop.view.activity.LoginActivity;
import com.example.heimashop.view.activity.OrderListActivity;
import com.example.heimashop.view.activity.SettingsActivity;

/**
 * 我的Fragment
 * 页面6: MineFragment - 个人中心页面
 */
public class MineFragment extends BaseFragment<FragmentMineBinding> {

    @Override
    protected FragmentMineBinding getViewBinding(LayoutInflater inflater, ViewGroup container) {
        return FragmentMineBinding.inflate(inflater, container, false);
    }

    @Override
    protected void initView() {
        // 检查登录状态
        boolean isLogin = SPUtils.getBoolean("isLogin", false);
        if (isLogin) {
            String username = SPUtils.getString("username", "用户");
            binding.tvUsername.setText(username);
        }
    }

    @Override
    protected void initData() {
        // 不需要初始化数据
    }

    @Override
    protected void initListener() {
        // 用户信息点击 - 跳转登录
        binding.llUserInfo.setOnClickListener(v -> {
            boolean isLogin = SPUtils.getBoolean("isLogin", false);
            if (!isLogin) {
                Intent intent = new Intent(getContext(), LoginActivity.class);
                startActivity(intent);
            }
        });

        // 订单相关
        binding.llOrderAll.setOnClickListener(v -> openOrderList("all"));
        binding.llOrderWaitPay.setOnClickListener(v -> openOrderList("wait_pay"));
        binding.llOrderWaitSend.setOnClickListener(v -> openOrderList("wait_send"));
        binding.llOrderWaitReceive.setOnClickListener(v -> openOrderList("wait_receive"));

        // 收藏
        binding.llFavorites.setOnClickListener(v -> {
            ToastUtils.showShort(requireContext(), "我的收藏");
        });

        // 地址
        binding.llAddress.setOnClickListener(v -> {
            Intent intent = new Intent(getContext(), AddressListActivity.class);
            startActivity(intent);
        });

        // 设置
        binding.llSettings.setOnClickListener(v -> {
            Intent intent = new Intent(getContext(), SettingsActivity.class);
            startActivity(intent);
        });
    }

    private void openOrderList(String status) {
        Intent intent = new Intent(getContext(), OrderListActivity.class);
        intent.putExtra("status", status);
        startActivity(intent);
    }
}
